<html>
<body>
<?php 

	if ( !extension_loaded("ming") ) 
		die ("PHP Erweiterung 'Ming' wird ben&ouml;tigt!"); 


	ini_set('max_execution_time','3600');

	ming_useswfversion(7);


	function buildContentMovie( $fileName, $width, $height, $background_rgb_array, $fps ) {

		$script = <<<EOF
stop(); 
nc=new NetConnection(); 
nc.connect(null); 
newStream=new NetStream(nc); 
video1.attachVideo(newStream); 
newStream.setBufferTime(15); 
newStream.play("norah12.flv"); 

this.createTextField("loaded_txt", this.getNextHighestDepth(), 10, 10, 200, 22);

this.createEmptyMovieClip("progressBar_mc", this.getNextHighestDepth());
progressBar_mc.createEmptyMovieClip("bar_mc", progressBar_mc.getNextHighestDepth());
with (progressBar_mc.bar_mc) {
    beginFill(0xFFFFFF);
    moveTo(0, 0);
    lineTo(100, 0);
    lineTo(100, 10);
    lineTo(0, 10);
    lineTo(0, 0);
    endFill();
    _xscale = 0;
}
progressBar_mc.createEmptyMovieClip("stroke_mc", progressBar_mc.getNextHighestDepth());
with (progressBar_mc.stroke_mc) {
    lineStyle(0, 0xffffff);
    moveTo(0, 0);
    lineTo(100, 0);
    lineTo(100, 10);
    lineTo(0, 10);
    lineTo(0, 0);
}

loaded_interval = setInterval(checkBytesLoaded, 500, newStream);
function checkBytesLoaded(my_ns) {
    var pctLoaded = Math.round(my_ns.bytesLoaded/my_ns.bytesTotal*100);

    loaded_txt.text = Math.round(my_ns.bytesLoaded/1024)+" of "+Math.round(my_ns.bytesTotal/1024)+" KiB loaded ("+pctLoaded+"%)";

    my_fmt = new TextFormat();
    my_fmt.color = 0xffffff;
    my_fmt.underline = true;
    loaded_txt.setTextFormat(my_fmt);

    progressBar_mc.bar_mc._xscale = pctLoaded;
    if (pctLoaded>=100) {
    clearInterval(loaded_interval);
    }
}

EOF;

		$action = new SWFAction($script);
		$stream = new SWFVideoStream();
		$stream->setDimension($width,$height);

		$movie = new SWFMovie();
		$movie->setBackground(
			$background_rgb_array[0],
			$background_rgb_array[0], 
			$background_rgb_array[0]
		);

		$movie->setDimension($width, $height);
		$movie->setRate($fps);

		$video_item = $movie->add($stream);
		$video_item->setname("video1");
		$action_item = $movie->add($action);

		$movie->save($fileName);

	};

	function swf_make_easyButton( $movie, $label, $xPos, $yPos, $action ) {
		
		$upShape = new SWFShape(); 
			$upShape->setRightFill(0,0,255); 
			$upShape->drawLine(250,0); 
			$upShape->drawLine(0,60); 
			$upShape->drawLine(-250,0); 
			$upShape->drawLine(0,-60);
 
		$overShape = new SWFShape(); 
			$overShape->setRightFill(255,0,0); 
			$overShape->drawLine(250,0); 
			$overShape->drawLine(0,60); 
			$overShape->drawLine(-250,0); 
			$overShape->drawLine(0,-60); 

		$hitShape = new SWFShape(); 
			$hitShape->setRightFill(255,0,255); 
			$hitShape->drawLine(250,0); 
			$hitShape->drawLine(0,60); 
			$hitShape->drawLine(-250,0); 
			$hitShape->drawLine(0,-60); 

		$myFont=new SWFFont("FreeSerif.fdb"); 

		$myButton=new SWFButton(); 
			$myButton->setUp($upShape); 
			$myButton->setOver($overShape); 
			$myButton->setDown($hitShape); 
			$myButton->setHit($hitShape); 
			$myButton->setAction(new SWFAction($action) );


		$button_item = $movie->add( $myButton );
		$button_item->moveTo($xPos, $yPos);
		$button_item->setName( $label );


		return $button_item; 

	}

	function swf_set_new_picture( $movie, $label, $xPos, $yPos, $fileName) {


		$shape = new SWFShape();

		$b = new SWFBitmap( fopen( $fileName, "r" ) );

	//	$fill_item = $shape->addFill( $b );

	//	$shape->setRightFill( $fill_item );
		//$shape->setColor( 0xff, 0xff, 0xff );
  	/*	$shape->drawLine(-10, 0);
  		$shape->drawLine(0, 100);
  		$shape->drawLine(-10, 0);
 		$shape->drawLine(0, 100); 

		$shape_item = $movie->add( $shape );
		$shape_item->moveTo( $xPos, $yPos );
		$shape_item->setName( $label ); 

		return $shape_item;*/

		echo "<p>".$b->getWidth()."x".$b->getHeight()."</p>";

		$shape_class = new SWFShape();
		$shape_class->setLine(1,0,0,0); 


		$shape_filled = $shape_class->addFill( $b, SWFFILL_TILED_BITMAP );

		$shape_class->setRightFill( $shape_filled );

/*
$shape_class->setLine(40, 0x7f, 0, 0);
$shape_class->movePenTo(200, 200);
$shape_class->drawLineTo(200, 200);
$shape_class->drawLineTo(200, 60);
$shape_class->drawCurveTo(200, 60, 200, 200);
*/

		$pic_item = $movie->add( $shape_class );
		$pic_item->moveTo( $xPos, $yPos );

		return $pic_item;

	}

	function swf_set_new_picture_as ( $movie, $fileName, $xPos, $yPos ) {

		$script = <<<EOF
createEmptyMovieClip('picxmms',10);
picxmms._x=$xPos;
picxmms._y=$yPos;
picxmms.loadMovie('$fileName');
EOF;

		$pic_item = $movie->add ( new SWFAction ( $script ) );

		//$pic_item->moveTo( $xPos, $yPos );

	}

	function swf_set_new_text( $movie, $label, $xPos, $yPos, $fontName, $height, $color_arr, $string ) {

		$text = new SWFText();
		$text->setFont( new SWFFont( $fontName ) );
		$text->setHeight( $height ); 
		$text->setColor(
			$color_arr[0],
			$color_arr[1],
			$color_arr[2]
		);
		$text->addString( $string );


		$text_item = $movie->add( $text );
		$text_item->moveTo( $xPos, $yPos );
		$text_item->setName( $label );

		return $text_item;

	}

	function buildGUILayeredMovie ( $contentMovie_fileName, $width, $height, $background_rgb_array, $fps, $guiMovie_fileName ) {

		$gui_width	= 0;
		$gui_height	= 100;
		$gui_fps	= $fps;

		
		$script = <<<EOF

this.createTextField("mouse_txt", this.getNextHighestDepth(), 0, 0, 150, 66);
mouse_txt.html = true;
mouse_txt.multiline = true;
row1_str = "&nbsp;\t<b>_xmouse\t</b><b>_ymouse</b>";
this.onMouseMove = function() {

    mouse_txt.htmlText = "<textformat tabStops='[50,100]'>";
    mouse_txt.htmlText += row1_str;
    mouse_txt.htmlText += "<b>_level0</b>\t"+_xmouse+"\t"+_ymouse;
    mouse_txt.htmlText += "<b>my_mc</b>\t"+this._xmouse+"\t"+this._ymouse;
    mouse_txt.htmlText += "</textformat>";

fmt = new TextFormat();
fmt.color = 0xff0000;
mouse_txt.setTextFormat( fmt );


};

EOF;


		$movieMain = new SWFMovie();
		$movieMain->setBackground(0x0, 0x0, 0x0);
		$movieMain->setDimension($width + $gui_width, $height + $gui_height);
		$movieMain->setRate($gui_fps);


		$movieContent_item = $movieMain->add ( new SWFPrebuiltClip ( fopen($contentMovie_fileName, "rb") ) );
		$movieContent_item->moveTo(0,0);
		$movieContent_item->setName("movieContent");


/*getURL('http://www.tupperworx.de.heimnetz/project/mingtest-video.php', '_top');*/

		$text_item = swf_set_new_text( $movieMain, "text1", 0, 400, "FreeSerif.fdb", 40, array(0xff, 0xff, 0xff),
			"Toll"
		);

		$text1_item = swf_set_new_text( $movieMain, "text2", 0, 400, "FreeSerif.fdb", 20, array(0xff, 0xff, 0xff),
			"Mein Pippimann juckt"
		);


		
		$gui_status_action = $movieMain->add( new SWFAction( $script ) );

		$button1_item_id = swf_make_easyButton( 
				$movieMain,
				"button1",
				10,
				490,
				"movieContent.newStream.seek(0);" 
		);

		$button2_item_id = swf_make_easyButton( 
				$movieMain,
				"button2",
				200,
				490,
				"movieContent.newStream.pause();" 
		);

		//$picture_item = swf_set_new_picture( $movieMain, "testpicture", 150, 150, "data/images/tupperworx_logo.dbl" );

		swf_set_new_picture_as( $movieMain, "tupperworx_logo.dbl", 150, 150 );

		for ($i = 0; $i < 600; $i++) {
        		$text_item->moveTo($i, 490);
        		$movieMain->nextFrame();
		}

		$movieMain->save($guiMovie_fileName);

		return array ( $gui_width+$width, $gui_height+$height );

	}


	buildContentMovie( "temp/MovieContent.swf", 720, 480, array(0,0,0), 25 );

	$movie_dimension_arr = buildGUILayeredMovie( "temp/MovieContent.swf", 720, 480, array(0,0,0), 25, "temp/GUIMovie.swf" );
	$width = $movie_dimension_arr[0];
	$height = $movie_dimension_arr[1];

	echo "<p>Current TimeStamp: ".time()."</p>";

	echo <<<EOF

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" ID=objects WIDTH=$width HEIGHT=$height>
<PARAM NAME=movie VALUE="temp/GUIMovie.swf">
<EMBED src="temp/GUIMovie.swf" WIDTH=$width HEIGHT=$height TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"> 
</OBJECT>


EOF;


?> 


</body>
</html>